package com.hd.trans.ui.model;


import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.hd.trans.common.Constants;
import com.hd.trans.common.interf.Callback;
import com.hd.trans.db.DataBaseMgr;
import com.hd.trans.db.bean.FileTranslateRecord;
import com.hd.trans.db.bean.HuDunLanguage;
import com.hd.trans.utils.FileUtil;

import java.util.Collections;
import java.util.List;


/**
 * 图片翻译页面数据
 */
public class PictureTransModel extends ViewModel {

    private MutableLiveData<HuDunLanguage> mLanguageFrom; // 源语言
    private MutableLiveData<HuDunLanguage> mLanguageTo; // 目标语言

//    Constants.TranslatePageState

    /**
     * 当前状态
     *
     * @see Constants.TranslatePageState
     */
    private MutableLiveData<Integer> mTranslateState;

    private MutableLiveData<FileTranslateRecord> mOperateRecord; // 当前操作记录

    private MutableLiveData<List<FileTranslateRecord>> listFileTranslateRecord = new MutableLiveData<>(); // 图片翻译历史记录

    public MutableLiveData<HuDunLanguage> getLanguageFrom() {
        if (mLanguageFrom == null) {
            mLanguageFrom = new MutableLiveData<>();
        }
        return mLanguageFrom;
    }

    public void setLanguageFrom(HuDunLanguage item) {
        if (mLanguageFrom == null) {
            mLanguageFrom = new MutableLiveData<>();
        }
        mLanguageFrom.setValue(item);
    }

    public MutableLiveData<HuDunLanguage> getLanguageTo() {
        if (mLanguageTo == null) {
            mLanguageTo = new MutableLiveData<>();
        }
        return mLanguageTo;
    }

    public void setLanguageTo(HuDunLanguage item) {
        if (mLanguageTo == null) {
            mLanguageTo = new MutableLiveData<>();
        }
        mLanguageTo.setValue(item);
    }


    public MutableLiveData<Integer> getTranslateState() {
        if (mTranslateState == null) {
            mTranslateState = new MutableLiveData<>();
        }
        return mTranslateState;
    }

    public void setTranslateState(Integer val) {
        if (mTranslateState == null) {
            mTranslateState = new MutableLiveData<>();
        }
        mTranslateState.setValue(val);
    }


    public MutableLiveData<FileTranslateRecord> getOperateRecord() {
        if (mOperateRecord == null) {
            mOperateRecord = new MutableLiveData<>();
        }
        return mOperateRecord;
    }

    public void setOperateRecord(FileTranslateRecord item) {
        if (mOperateRecord == null) {
            mOperateRecord = new MutableLiveData<>();
        }
        mOperateRecord.setValue(item);
    }

    public MutableLiveData<List<FileTranslateRecord>> getListFileTranslateRecord() {
        return listFileTranslateRecord;
    }

    public void setListTranslateRecord(List<FileTranslateRecord> listFileTranslateRecord) {
        this.listFileTranslateRecord.setValue(listFileTranslateRecord);
    }

    public void getFileTranslateRecordByTime(long createTime){
        FileTranslateRecord record = DataBaseMgr.getInstance().getFileTranslateRecordByTime(createTime);
        setOperateRecord(record);
    }

    public void getFileTranslateRecord(){
        List<FileTranslateRecord> records = DataBaseMgr.getInstance().getFileTranslateRecord();
        Collections.reverse(records);//排序
        setListTranslateRecord(records);
    }

    public void deleteFileTranslateRecord(FileTranslateRecord record, Callback callback){
        DataBaseMgr.getInstance().deleteFileTranslateRecord(record);
        //删除文件
        FileUtil.deleteFile(record.getOriginalFilePath());
        FileUtil.deleteFile(record.getTranslateFilePath());
        FileUtil.deleteFile(record.getOriginalText());
        FileUtil.deleteFile(record.getTranslateText());
        FileUtil.deleteFile(record.getMetaFilePath());
        FileUtil.deleteFile(record.getInpaintedFilePath());
        if(callback != null) callback.onCallback();
    }

    public void deleteFileTranslateRecords(List<FileTranslateRecord> records, Callback callback){
        for(FileTranslateRecord record:records){
            deleteFileTranslateRecord(record, null);
        }
        if(callback != null) callback.onCallback();
    }

}